<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>仓库编辑页</title>
  <link rel="stylesheet" href="/layui/css/layui.css">
  <link rel="stylesheet" href="/stylesheets/base.css">
  <script src="/layui/layui.js"></script>
  <script src="/javascripts/jquery.js"></script>
  <script src="/javascripts/ajax.js"></script>
</head>
<body>
<form id="editForm" class="layui-form" style="margin: 10px">
  <input type="hidden" id="id" name="id">
  <div class="layui-form-item">
    <label class="layui-form-label" for="name"><span class="red">*</span>仓库名</label>
    <div class="layui-input-block">
      <input type="text" name="name" id="name" placeholder="请输入仓库名" class="layui-input" lay-verify="required">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label" for="address">地址</label>
    <div class="layui-input-block">
      <textarea class="layui-textarea" name="address" id="address" placeholder="请输入地址"></textarea>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label" for="remake">备注</label>
    <div class="layui-input-block">
      <textarea class="layui-textarea" name="remake" id="remake" placeholder="请输入备注"></textarea>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label" for="role"><span class="red">*</span>状态</label>
    <div class="layui-input-block">
      <select name="status" id="status">
        <option value="1">可用</option>
        <option value="2">禁用</option>
      </select>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label"></label>
    <div class="layui-input-block">
      <button type="submit" class="layui-btn" lay-filter="submitBtn" lay-submit>提交</button>
    </div>
  </div>
</form>
</body>
<script>
  const {form} = layui;
  $(function () {
    const id = location.search.substring(location.search.indexOf("=")+1);
    if(id){
      ajax.get("/store/info/"+id).then(data=>{
        $("#editForm input,select,textarea").each((index,item)=>{
          const name = $(item).prop("id");
          for(let key in data){
            if(name === key){
              $("#"+name).val(data[key]);
            }
          }
        })
        form.render();
      })
    }

    form.on("submit(submitBtn)",function (data) {
      ajax.post("/store/update",data.field).then(()=>{
        layer.alert(`${id?"修改":"添加"}成功！`,{icon:6},function (){
          closeWin();
          parent.reloadTable();
        })
      })
      return false;
    })
  })

  function closeWin() {
    const index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
    parent.layer.close(index);
  }
</script>
</html>
